package topic1

func main() {

}

type ListNode struct {
	Val  int
	Next *ListNode
}

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	list := &ListNode{0, nil}
	next := list
	for l1 != nil || l2 != nil {
		next.Next = &ListNode{0, nil}
		if l1 != nil {
			next.Val += l1.Val
			l1 = l1.Next
		}
		if l2 != nil {
			next.Val += l2.Val
			l2 = l2.Next
		}

		if next.Val >= 10 {
			next.Val -= 10
			next.Next.Val = 1
		} else if l1 == nil && l2 == nil {
			next.Next = nil
		}
		next = next.Next
	}
	return list
}
